#include "hollowWidget.h"
#include <BRepOffsetAPI_MakeThickSolid.hxx>

hollowWidget::hollowWidget(modelSoft* myModelSoft, QWidget *parent)
	: myModelSoft(myModelSoft)
	, QWidget(parent)
	, ui(new Ui::hollowWidgetClass())
{
	ui->setupUi(this);
	ui->modifyButton->setEnabled(false);
	ui->confirmButton->setEnabled(false);
	myModelSoft->myView->getInteractorStyle()->setHollowOperation(true);
	isModified = false;
	hollowFeature = new HollowFeature();
	hollowFeature->setHollowId(myModelSoft->shapeTag++);
	hollowFeature->setShapeId(hollowFeature->getHollowId());
	selectType = 0;
	hollowFeature->setHollowD(0.0);
	D = 0.0;
	hollowFeature->setHollowTolerance(0.0);
	tolerance = 0.0;
	createConnection();
}

hollowWidget::~hollowWidget()
{
	delete ui;
}

void hollowWidget::createConnection()
{
	connect(myModelSoft->currFaceSelector, &OccFaceSelector::sentHollowShape, this, &hollowWidget::receiveHollowSelectedShape);
	connect(myModelSoft->currShapeSelector, &OccShapeSelector::sentHollowShape, this, &hollowWidget::receiveHollowSelectedShape);
	connect(ui->modifyButton, &QPushButton::clicked, this, &hollowWidget::onClickModify);
	connect(ui->confirmButton, &QPushButton::clicked, this, &hollowWidget::onClickConfirm);
	connect(ui->cancelButton, &QPushButton::clicked, this, &hollowWidget::close);
	connect(ui->shapeSelect, &QPushButton::clicked, this, &hollowWidget::onClickShapeSelect);
	connect(ui->cancelShapeSelect, &QPushButton::clicked, this, &hollowWidget::OnCancelSelShape);
	connect(ui->faceSelect, &QPushButton::clicked, this, &hollowWidget::onClickFaceSelect);
	connect(ui->cancelFaceSelect, &QPushButton::clicked, this, &hollowWidget::OnCancelSelFace);

}

void hollowWidget::onClickModify()
{
	D = ui->dInput->text().toDouble();
	tolerance = ui->tolInput->text().toDouble();
	hollowFeature->setHollowD(D);
	hollowFeature->setHollowTolerance(tolerance);
	//判断厚度是否有效 以及是否被修改过
	if (!IsNearlyZero(D) && !isModified) {
		BRepOffsetAPI_MakeThickSolid aSolidMaker;
		aSolidMaker.MakeThickSolidByJoin(selectPipeline->getShape(), facesToRemove, D, tolerance);
		const TopoDS_Shape newShape = aSolidMaker.Shape();

		resultPipeline = myModelSoft->CreatePipeline(newShape, hollowFeature->getHollowId(), true);
		hollowFeature->setResultId(resultPipeline->GetShapeID());
		selectPipeline->SetPipelineVisibility(false);
		myModelSoft->myView->getRenderWindow()->Render();
		isModified = true;
		ui->confirmButton->setEnabled(true);
		ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,255,255)");
		myModelSoft->myView->getInteractorStyle()->clearCurrSelector();
	}

}

void hollowWidget::onClickConfirm()
{
	QStandardItem* item1 = new QStandardItem(QString("挖空-%1").arg(hollowFeature->getHollowId()));
	int row = myModelSoft->ui->model->rowCount();
	item1->setEditable(false);
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	myModelSoft->m_Features.insert(std::pair<Standard_Integer, HollowFeature*>(hollowFeature->getHollowId(), hollowFeature));
	myModelSoft->RemovePipelineFromRenderer(selectPipeline);
	hollowFeature->AddOperation(hollowFeature);
	confirmCreated = true;
	close();

}

//void hollowWidget::onClickCancel()
//{
//	//myModelSoft->myView->getInteractorStyle()->getSelectedPipelines
//   // 清除已选择的Actor
//	myModelSoft->myView->getInteractorStyle()->clearCurrSelector();
//	myModelSoft->myView->getInteractorStyle()->setHollowOperation(false);
//	// 没有点确定，删除创建的Actor
//	if (!confirmCreated)
//	{
//		if (!resultPipeline.IsNull())
//		{
//			myModelSoft->RemovePipelineFromRenderer(resultPipeline);
//			myModelSoft->myView->getRenderWindow()->Render();
//		}
//	}
//
//	close();
//
//}

void hollowWidget::closeEvent(QCloseEvent* event)
{
// 清除已选择的Actor
	myModelSoft->myView->getInteractorStyle()->clearCurrSelector();
	myModelSoft->myView->getInteractorStyle()->setHollowOperation(false);
	// 没有点确定，删除创建的Actor
	if (!confirmCreated)
	{
		if (!selectPipeline.IsNull()) {
			selectPipeline->SetPipelineVisibility(true);
		}
		
		if (!resultPipeline.IsNull())
		{
			myModelSoft->RemovePipelineFromRenderer(resultPipeline);
			myModelSoft->myView->getRenderWindow()->Render();
		}
	}

	close();

}

void hollowWidget::onClickShapeSelect()
{
    selectType = 0;
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Solid);
	myModelSoft->changeSelector(SM_Solid);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);

}

void hollowWidget::onClickFaceSelect()
{
	selectType = 1;
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Face);
	myModelSoft->changeSelector(SM_Face);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);
	//恢复体选择的状态
	if (shapeReceive) {
		myModelSoft->RemoveSelectedActor(selectPipeline);
	}
}

void hollowWidget::OnCancelSelShape()
{
}

void hollowWidget::OnCancelSelFace()
{
}


void hollowWidget::receiveHollowSelectedShape(const TopoDS_Shape& subShape, Handle(QIVtkSelectionPipeline) pipeline)
{
	if (selectType == 1 && (pipeline->GetShapeID() == selectPipeline->GetShapeID()) && !faceReceive) {
		facesToRemove.Append(TopoDS::Face(subShape));
		//查找选择边在父对象的索引值
		//映射容器，用于存储 TopoDS_Shape 对象的索引映射
		TopTools_IndexedMapOfShape M;
		//遍历 S 中的所有边	将边的索引添加到 M 中
		TopExp::MapShapes(pipeline->getShape(), TopAbs_FACE, M);
		int count = M.Extent();
		while (count >= 0) {
			if (M.FindKey(count).IsEqual(subShape)) {
				hollowFeature->addSubShapeIndex(count);
				break;
			}
			count--;
		}
		faceReceive = true;
		ui->modifyButton->setEnabled(true);
		ui->faceSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");

	}
	else if (selectType == 0 && !shapeReceive) {
		selectPipeline = pipeline;
		auto iter = myModelSoft->m_Features.find(pipeline->GetShapeID());
		if (iter == myModelSoft->m_Features.end()) {
			return;
		}
		hollowFeature->setTargetFeature(iter->second);
		ui->shapeSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");
		shapeReceive = true;
	}

	if (faceReceive && shapeReceive) {
		ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");
	}
}
